# 员工信息管理的视图文件
from django.shortcuts import render
from django.http import HttpResponse
from myadmin.models import User
from django.core.paginator import Paginator #导入翻页模块
from django.db.models import Q # 支持或的条件
from datetime import datetime
# Create your views here.

def index(request,ppindex=1):
    # 浏览信息
    umod = User.objects # 获取实例化操作对象
    # userlist=umod.all() #获取遍历对象的信息
    userlist1 = umod.filter(status__lt=9) # 查找除status__lt=9之外的数据
    
    #获取并判断搜索条件
    mywhere=[]
    kw=request.GET.get("keyword",None) #获取网页上传过来的搜索的值
    if kw:
        userlist1=userlist1.filter(Q(username__contains=kw)|Q(nickname__contains=kw)) #__contains 模糊查询,Q表示 或 的条件
        mywhere.append('keyword='+kw)
    # 执行分页处理
    ppindex = int(ppindex)
    page=Paginator(userlist1,5) #每页的数据条数
    maxppages=page.num_pages # 获取总页数
    #判断当前页是否越界
    
    if ppindex > maxppages:
        ppindex = maxppages
    if ppindex < 1:
        ppindex = 1
    userlist = page.page(ppindex) #获取当前页数据
    plist = page.page_range #获取页码列表信息
    
    # contest = {'userlist':userlist} #封装对象的信息
    contest = {"userlist":userlist,'plist':plist,'ppindex':ppindex,'maxppages':maxppages,'mywhere':mywhere} #封装对象的信息
    return render(request,'myadmin/user/index.html',contest) # 把封装的信息加载到模板里面

def add(request):
    return render(request,'myadmin/user/add.html')

def insert(request):
    # 执行信息添加
    try:
        ob = User()
        ob.username = request.POST['username']
        ob.nickname = request.POST['nickname']
        # 密码的md5密文的处理
        import hashlib,random
        md5 = hashlib.md5() # 
        n = random.randint(100000,999999) # 获取一个随机数（干扰值）
        s = request.POST['password']+str(n) # 密码和干扰值一起并装
        md5.update(s.encode('utf-8')) # 转化编码模式
        ob.password_hash = md5.hexdigest() # 获取md5值
        ob.password_salt = n
        # end
        ob.status = 1
        ob.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info':"添加成功！"}
    except Exception as err:
        print(err)
        context = {'info':"添加失败！"}
    return render(request,"myadmin/info.html",context)
def delete(request,uid=0):
    # 执行信息删除
    try:
        ob = User.objects.get(id=uid) # 获取表单的数据id=uid
        ob.status = 9 # 修改状态
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info':"删除成功！"}
    except Exception as err:
        print(err)
        context = {'info':"删除失败！"}
    return render(request,"myadmin/info.html",context)

def edit(request,uid=0):
    # 加载信息编辑表单
    try:
        ob = User.objects.get(id=uid) # 获取表单的数据id=uid\
        context = {'user':ob}
        return render(request,"myadmin/user/edit.html",context)

    except Exception as err:
        print(err)
        context = {'info':"没有找到修改的数据信息"}
        return render(request,"myadmin/info.html",context)

def update(request,uid):
    # 执行信息编辑（修改）
    try:
        ob = User.objects.get(id=uid) # 获取表单的数据id=uid
        ob.nickname = request.POST['nickname']
        ob.status = request.POST['status']
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info':"修改成功！"}
    except Exception as err:
        print(err)
        context = {'info':"修改失败！"}
    return render(request,"myadmin/info.html",context)
